package bsh.dyn;

import org.objectweb.asm.MethodVisitor;

import bsh.CompilationState;
import bsh.CompiledScript;
import bsh.exception.CompilationFailedException;

/**
 * {@link TransformationList} that is empty. Should be used as a basis. Is a {@link TransformationList#isCompilable()} if the underlying
 * {@link Transformation} is compilable
 * 
 * @author takeshi
 */
public class EmptyTransformationList implements TransformationList, CompilableTransformation {
    private final Transformation transformation;

    /**
     * C'tor
     * 
     * @param transformation {@link Transformation}
     */
    public EmptyTransformationList(Transformation transformation) {
        super();
        this.transformation = transformation;
    }

    /**
     * {@inheritDoc}
     */
    public ActualParameter getTransformationTarget() {
        return this.transformation.getTransformationTarget();
    }

    /**
     * {@inheritDoc}
     */
    public int getWeight() {
        return this.transformation.getWeight();
    }

    /**
     * {@inheritDoc}
     */
    public TransformationList appendTransformation(Transformation another) {
        return new UnmodifiableTransformationList(another, this);
    }

    /**
     * {@inheritDoc}
     */
    public boolean acceptsType(ActualParameter parameter) {
        return this.transformation.acceptsType(parameter);
    }

    /**
     * {@inheritDoc}
     */
    public Object transform(Object object, CompiledScript script) {
        return this.transformation.transform(object, script);
    }

    /**
     * {@inheritDoc}
     */
    public boolean isCompilable() {
        return this.transformation.isCompilable();
    }

    /**
     * {@inheritDoc}
     */
    public void compile(MethodVisitor visitor, CompilationState state) throws CompilationFailedException {
        if (this.transformation.isCompilable()) {
            ((CompilableTransformation) this.transformation).compile(visitor, state);
        } else {
            throw new IllegalArgumentException("not compilable!");
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "TransformationList [transformation=" + transformation + "]";
    }

}